package com.example.redis;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class Client2 {
    /**
     * 集群连接
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        //创建一连接，JedisCluster对象,在系统中是单例存在
        Set<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort("192.168.242.129", 7001));
        nodes.add(new HostAndPort("192.168.242.129", 7002));
        nodes.add(new HostAndPort("192.168.242.129", 7003));
        nodes.add(new HostAndPort("192.168.242.129", 7004));
        nodes.add(new HostAndPort("192.168.242.129", 7005));
        nodes.add(new HostAndPort("192.168.242.129", 7006));
        JedisCluster cluster = new JedisCluster(nodes);
        //执行JedisCluster对象中的方法，方法和redis一一对应。
        cluster.set("cluster-test", "my jedis cluster test");
        String result = cluster.get("cluster-test");
        System.out.println(result);
        //程序结束时需要关闭JedisCluster对象
        cluster.close();
    }
}
